package com.example.homescreen.model;

import java.util.List;

import com.example.homescreen.R;
import com.example.homescreen.entity.Food;

import android.location.Geocoder;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.location.Address;

public class DetailActivity extends Activity {
	Food food;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);
		Resources res = getResources();
		food = new Food();
		food = (Food) getIntent().getSerializableExtra("food");
		ImageView imga = (ImageView) findViewById(R.id.image_View);
		TextView name = (TextView) findViewById(R.id.text_Name);
		TextView addess = (TextView) findViewById(R.id.text_Address);
		TextView des = (TextView) findViewById(R.id.textView_Des);
		TextView price = (TextView) findViewById(R.id.text_Price);
		des.setMovementMethod(new ScrollingMovementMethod());
		String image = food.getfImage();
		imga.setImageResource(res.getIdentifier(
				"com.example.homescreen:drawable/" + image, null, null));
		name.setText(food.getfName());
		addess.setText(food.getfAddress());
		des.setText(food.getfDescription());
		price.setText(food.getfPrice() + " đồng/suất");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.detail, menu);
		return true;
	}

	public boolean isOnline() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnectedOrConnecting()) {
			return true;
		}
		return false;
	}

	public void displayMap(View v) {
		if (isOnline() == false) {
			Toast.makeText(
					getApplicationContext(),
					"No network connection! Please connect to network and try again!",
					Toast.LENGTH_LONG).show();
		} else {
			food = new Food();
			food = (Food) getIntent().getSerializableExtra("food");
			Geocoder coder = new Geocoder(this);
			List<Address> address;
			try {
				address = coder.getFromLocationName(food.getfAddress().trim(),
						5);
				if (address == null) {
					return;
				}
				Address location = address.get(0);
				location.getLatitude();
				location.getLongitude();
				Log.i("Dia Chi", "" + location.getLatitude() + " va "
						+ location.getLongitude());
				Intent intent = new Intent(DetailActivity.this,
						GoogleMapDisplay.class);
				intent.putExtra("Latitude", location.getLatitude());
				intent.putExtra("Longitude", location.getLongitude());
				intent.putExtra("Address", food.getfAddress());
				startActivity(intent);

			} catch (Exception e) {
				Log.i("GG", e.getMessage());
			}

		}

	}

}
